package com.itlong.cloud.tapd.service;

import com.itlong.cloud.tapd.common.Result;
import com.itlong.cloud.tapd.util.GetTime;
import com.itlong.cloud.tapd.util.HttpSendUtil;
import com.itlong.cloud.tapd.util.Special;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <des>
 *
 * <des>
 *
 * @create Date 2021-09-10
 **/
@Service
public class TotalService {
    List<String> userList= new ArrayList<>();


    List<String> workspaceidList = new ArrayList<>();

    public    List<String> getUserList(){
        if(userList.isEmpty()) {
            synchronized (CompletedWorkOrdersNumberService.class) {
                if(userList.isEmpty()) {
                    userList = getUserWorkspace();
                }
            }
        }
        return userList;
    }

    public  List<String> getWorkspaceidList(){
        if(workspaceidList.isEmpty()) {
            synchronized (CompletedWorkOrdersNumberService.class) {
                if(workspaceidList.isEmpty()) {
                    workspaceidList = getWorkspacesProjects();
                }
            }
        }
        return workspaceidList;
    }

    /**
     * <desc>
     *      获取公司成员名字列表
     * </desc>
     * @param
     * @return: java.util.List<com.itlong.cloud.tapd.common.POJO.UserWorkspacePO>
     * @author LiuYu
     * @date: 2021-09-07
     **/
    public  List<String> getUserWorkspace(){
        String url = "https://api.tapd.cn/workspaces/users?workspace_id=52784121";
        Result result =HttpSendUtil.send(url);
        Iterator iterator =  ((List<Object>) result.getData()).iterator();
        while (iterator.hasNext()){
            HashMap<String,Object> map = (HashMap<String, Object>) iterator.next();
            HashMap<String,Object> map1 = (HashMap<String, Object>) map.get("UserWorkspace");
            String user = (String) map1.get("user");
            if(!("黄永康,蒋利文,丘彬,伍永德,丁祥松,张先永,雷文超,赖日照,黄丽云,程金古,郑怀进,韦耀甘,任冬梅,ITLYFWM," +
                    "万小龙,刘芸利,史世锋,罗泽强,吴国刚,唐亮,安青丰,尹小斌,张学盈,张清明,徐浩,李子良,李晓彤,江杨波,王启辉,程洪硕,简土明,薛茫,谭辉沛,郑礼晓,陈秋林,陈秋林aQiuLin").contains(user)) {
                userList.add(user);
            }
        }
        return userList;
    }

    /**
     * <desc>
     *      获取公司所有项目id
     * </desc>
     * @param
     * @return: java.util.List<java.lang.String>
     * @author LiuYu
     * @date: 2021-09-07
     **/
    public  List<String> getWorkspacesProjects(){
        String url = "https://api.tapd.cn/workspaces/projects?company_id=52784121";
        Result result =HttpSendUtil.send(url);
        Iterator iterator =  ((List<Object>) result.getData()).iterator();
        while (iterator.hasNext()){
            HashMap<String,Object> map = (HashMap<String, Object>) iterator.next();
            HashMap<String,Object> map1 = (HashMap<String, Object>) map.get("Workspace");
            workspaceidList.add((String) map1.get("id"));
        }
        return workspaceidList;
    }


    /**
     * 获取从上上个月一号开始后的任务
     * @return
     */
    public List<HashMap<String,Object>> getTasks(List<String> workspaceidList){
        List<HashMap<String,Object>> list = new ArrayList<>();
        String url = new String();
        for (String workspace_id:workspaceidList){
            int page = 1;
            while (true) {
                url = "https://api.tapd.cn/tasks?workspace_id=" + workspace_id + "&created=>" + GetTime.getFirstDayOfLast6Month() + "&limit=200" + "&page=" + page;
                Result result = HttpSendUtil.getSend(url);
                List<Object> data = ((List<Object>) result.getData());
                Iterator iterator = data.iterator();
                while (iterator.hasNext()) {
                    HashMap<String, Object> map = (HashMap<String, Object>) iterator.next();
                    HashMap<String, Object> map1 = (HashMap<String, Object>) map.get("Task");
                    HashMap<String, Object> map2 = new HashMap<>();
                    map2.put("id", map1.get("id"));
                    map2.put("owner", map1.get("owner"));
                    map2.put("status", map1.get("status"));
                    map2.put("completed", map1.get("completed"));
                    map2.put("due", map1.get("due"));
                    map2.put("begin", map1.get("begin"));
                    list.add(map2);
                }
                page++;
                System.out.println(((List<Object>) result.getData()).size());
                if(((List<Object>) result.getData()).size()<200){
                    break;
                }
            }
        }
        return list;
    }
    /**
     * 获取从上上个月一号开始后的需求
     *
     * @return
     */
    public List<HashMap<String, Object>> getStories(List<String> workspaceidList) {
        List<HashMap<String, Object>> list = new ArrayList<>();
        String url = new String();
        for (String workspace_id : workspaceidList) {
            int page = 1;
            while (true) {
                url = "https://api.tapd.cn/stories?workspace_id=" + workspace_id + "&created=>" + GetTime.getFirstDayOfLast6Month() + "&limit=200"+"&page=" + page;
                Result result = HttpSendUtil.getSend(url);
                Iterator iterator = ((List<Object>) result.getData()).iterator();
                while (iterator.hasNext()) {
                    HashMap<String, Object> map = (HashMap<String, Object>) iterator.next();
                    HashMap<String, Object> map1 = (HashMap<String, Object>) map.get("Story");
                    HashMap<String, Object> map2 = new HashMap<>();
                    map2.put("id", map1.get("id"));
                    map2.put("owner", map1.get("owner"));
                    map2.put("status", map1.get("status"));
                    map2.put("completed", map1.get("completed"));
                    map2.put("due", map1.get("due"));
                    map2.put("begin", map1.get("begin"));
                    list.add(map2);
                }
                page++;
                if (((List<Object>) result.getData()).size() < 200) {
                    break;
                }
            }
        }
        return list;
    }

    /**
     * <desc>
     * 获取从上上个月一号开始后的缺陷
     * </desc>
     *
     * @param workspaceidLiist
     * @return: java.util.List<java.util.HashMap < java.lang.String , java.lang.Object>>
     * @author LiuYu
     * @date: 2021-09-08
     **/
    public List<HashMap<String, Object>> getBugs(List<String> workspaceidLiist) {
        List<HashMap<String, Object>> list = new ArrayList<>();
        String url = new String();
        for (String workspace_id : workspaceidLiist) {
            int page =1;
            while (true) {
                url = "https://api.tapd.cn/bugs?workspace_id=" + workspace_id + "&created=>" + GetTime.getFirstDayOfLast6Month() + "&limit=200"+"&page="+page;
                Result result = HttpSendUtil.getSend(url);
                Iterator iterator = ((List<Object>) result.getData()).iterator();
                while (iterator.hasNext()) {
                    HashMap<String, Object> map = (HashMap<String, Object>) iterator.next();
                    HashMap<String, Object> map1 = (HashMap<String, Object>) map.get("Bug");
                    HashMap<String, Object> map2 = new HashMap<>();
                    map2.put("id", map1.get("id"));
                    map2.put("owner", map1.get("current_owner"));
                    map2.put("status", map1.get("status"));
                    map2.put("resolved", map1.get("resolved"));
                    map2.put("due", map1.get("due"));
                    map2.put("begin", map1.get("begin"));
                    list.add(map2);
                }
                page++;
                if (((List<Object>) result.getData()).size() < 200) {
                    break;
                }
            }
        }
        return list;
    }


    /**
     * <desc>
     *     结果集
     * </desc>
     * @param userList
     * @param taskList
     * @param storyList
     * @param bugList
     * @return: java.util.HashMap<java.lang.String,java.lang.Object>
     * @author LiuYu
     * @date: 2021-09-10
     **/
    public Map<String,Object> getDailyResults(List<String> userList
            ,List<HashMap<String, Object>> taskList, List<HashMap<String, Object>> storyList, List<HashMap<String, Object>> bugList) throws ParseException {
        //近一周完成的单数
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //结果
        Map<String,Object> result = new HashMap<>();
        //告警人员列表,【1、统计所有成员近一周完成的工单数】
        Map<String,Integer> taskDoneLastWeekMap = new HashMap<>();
        //告警人员列表,【2、统计所有成员未来一周的open状态的单数】
        Map<String,Integer> taskToDoNextWeekMap = new HashMap<>();
        //告警人员列表,【3、统计任务单工作延迟的数据】
        Map<String,Integer> taskDelayMap = new HashMap<>();

        HashMap<String,Integer> map=new HashMap<>();
        Integer count = 0;
        for (String user:userList){
            //告警人员列表,【1、统计所有成员近一周完成的工单数】
            map=Special.getTaskDoneCountInSevenDays(user,taskList,storyList,bugList);
            count= map.get("taskCount");
            if(count<2){
                taskDoneLastWeekMap.put(user,count);
            }
            // 告警人员列表,【2、统计所有成员未来一周的open状态的单数】
            map = Special.getTaskToDoCountInNextSevenDays(user,taskList,storyList,bugList);
            count = map.get("taskCount");
            if(count<2){
                taskToDoNextWeekMap.put(user,count);
            }
            //告警人员列表,【3、统计任务单工作延迟的数据】
            map = Special.getTaskUnFinishedCount(user,taskList,storyList,bugList);
            count = map.get("taskCount");
            if(count>=1){
                taskDelayMap.put(user,count);
            }

        }
        result.put("【1、统计所有成员近一周完成的工单数<2】",taskDoneLastWeekMap);
        result.put("【2、统计所有成员未来一周的open状态的单数<2】",taskToDoNextWeekMap);
        result.put("【3、统计任务单工作延迟的数据>1】",taskDelayMap);
        return result;
    }

    /**
     * <desc>
     *     结果集
     * </desc>
     * @param userList
     * @param taskList
     * @param storyList
     * @param bugList
     * @return: java.util.HashMap<java.lang.String,java.lang.Object>
     * @author LiuYu
     * @date: 2021-09-10
     **/
    public Map<String,Object> getMonthlyResults(List<String> userList
            ,List<HashMap<String, Object>> taskList, List<HashMap<String, Object>> storyList, List<HashMap<String, Object>> bugList) throws ParseException {
        //告警人员列表,【4、月度单数统计】
        Map<String, String> taskDoneMonthlyMap = new HashMap<>();
        //告警人员列表,【5、月度缺单统计】
        Map<String, String> taskMissMonthlyMap = new HashMap<>();
        //结果
        Map<String,Object> result = new HashMap<>();

        HashMap<String, Integer> map = new HashMap<>();

        Integer count = 0;
        for (String user : userList) {
            //告警人员列表,【4、月度单数统计】
            map = Special.getTaskCountLastMonth(user, taskList, storyList, bugList);
            Integer finishCount = map.get("taskDoneCount");
            Integer lateCount = map.get("taskDoneInTimeCount") + map.get("taskDelayCount");
            HashMap<String, String> map6 = new HashMap<>();
            taskDoneMonthlyMap.put(user, "完成" + finishCount + "," + "超期" + lateCount);
            //告警人员列表,【5、月度缺单统计】
            map = Special.getMissTaskCountLastMonth(user, taskList, storyList, bugList);
//           count = map.get("count1")+map.get("count2")+map.get("count3");
            count = map.get("count");
            int miss = count;
            //构造方法的字符格式这里如果小数不足2位,会以0补足.
            DecimalFormat decimalFormat = new DecimalFormat(".00");
            String missrate = miss + "%";
            if (miss > 0) {
                missrate = decimalFormat.format(((float) miss / map.get("workdayCount")) * 100) + "%";
            }
            taskMissMonthlyMap.put(user, miss + "   " + "缺单率" + "," + missrate);
        }
        result.put("【4、月度单数统计】",taskDoneMonthlyMap);
        result.put("【5、月度缺单统计】",taskMissMonthlyMap);
        return result;
    }
}
